var config = require('../../../config/config.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    aid: 1,
    sid: 1,
    orderDisplay: true,
    isClick: false,
    shop_name: '',
    pageUrl: config.order_index,
    totalPrice: 0,
    flag: 1,
    new_flag: 1,
    order_list: [],
    scrollTop: 0,
    order_detail_list:[],
  },
  //随便逛逛
  buttonto: function () {
    wx.redirectTo({
      url: '../../shopCity/shopCity'
    })
  },
  // 触发bindscrolltolower事件时加载更多数据，先做判断，如果分页存在，则调用下面请求数据的方法，不存在则不调用
  getOrderUrl: function (message) {
    if (this.data.pageUrl) {
      this.getOrderUrl();
    }
  },
 
  /**
 * 页面相关事件处理函数--监听用户下拉动作
 */
  onPullDownRefresh: function () {
  },

  //请求列表
  getOrderUrl: function (e) {
    var that = this;
    var url = that.data.pageUrl;
    var type = that.data.flag;
    var new_type = that.data.new_flag;
    if (e) {
      if(!e.currentTarget.dataset.scroll){
        new_type = parseInt(e.currentTarget.dataset.type);
        if (new_type == type) {
          return;
        }
        that.setData({
          new_flag: new_type,
          scrollTop: 0
        })
        //将商品分类的值传给下面商品列表，通过对应的type显示对应的内容
        url = config.order_index + "?type=" + new_type;
      }
    }
    //判断下一页 的地址是否存在。如果判断条件为真（下一页接口不存在），则终止整个循环
    if (!url) {
      return;
    }
    var member_info = (wx.getStorageSync('member_info'));
    wx.request({
      url: url,
      data: {
        type: this.data.new_flag,
        aid: member_info.aid,
        mid: member_info.mid,
        sid: member_info.sid,
      },
      method: 'GET',
      success: function (res) {
        var order_list = that.data.order_list;
        var index = order_list.length;
        var goodsList = res.data.data.order_list.data;
        if (new_type != type) {
          index = 0;
          order_list = [];
        }
        for (var i = 0; i < goodsList.length; i++) {
          order_list[index + i] = goodsList[i];
        }
        that.setData({
          order_list: order_list,
          pageUrl: res.data.data.order_list.next_page_url,
          flag: new_type
        })
        if(type){
          if (that.data.order_list.length < 1) {
            that.setData({
              orderDisplay: false
            })
          }else{
            that.setData({
              orderDisplay: true
            })
          }
        }
      },
      fail: function () {
      },
    })
  },
  //搜索
  // formSubmit: function (e) {
  //   var member_info = (wx.getStorageSync('member_info'));
  //   var that = this;
  //   wx.request({
  //     url: config.order_index,
  //     data: {
  //       keyword: e.detail.value.keyword,
  //       aid: this.data.aid,
  //       mid: member_info.id,
  //       sid: this.data.sid,
  //     },
  //     method: 'GET',
  //     success: function (res) {
  //     },
  //     fail: function () {
  //     },
  //   })
  // },
  //删除订单
  cancal_pay: function (e) {
    var member_info = (wx.getStorageSync('member_info'));
    var that = this;
    wx.request({
      url: config.order_delete,
      data: {
        id: e.currentTarget.dataset.id,
        aid: member_info.aid,
        mid: member_info.mid,
        sid: member_info.sid,
      },
      method: 'GET',
      success: function (res) {
        //将删去的一行从数组中隐藏
        var order_list = that.data.order_list;
        var index = e.currentTarget.dataset.index;
        order_list.splice(index, 1);
        that.setData({
          order_list: order_list
        })
        wx.showToast({
          title: "取消成功",
        });
      },
      fail: function () {
      },
    })
  },
  //去支付
  to_pay: function (e) {
    var member_info = (wx.getStorageSync('member_info'));
    var that = this;
    wx.request({
      url: config.order_pay,
      method: "POST",
      data: {
        aid: member_info.aid,
        mid: member_info.mid,
        sid: member_info.sid,
        order_id: e.currentTarget.dataset.id,
        openid: member_info.openid
      },
      success: function (res) {
        if (!res.data.status) {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false
          })
        } else {
          that.Payment(res.data.data)
        }
      },
      fail: function (res) {
        console.log(res.data)
      }
    })
  },
  //微信支付
  Payment: function (data) {
    var ispaysuccess = 0;
    wx.requestPayment({
      'timeStamp': String(data.timeStamp),
      'nonceStr': data.nonceStr,
      'package': data.package,
      'signType': data.signType,
      'paySign': data.paySign,
      'success': function (res) {
        ispaysuccess = 1;
        wx.redirectTo({
          url: '../../shopCity/shopCity-detial/pay-success/pay-success?ispaysuccess=' + ispaysuccess,
        })
      },
      'fail': function (res) {
        // wx.showToast({ title: '支付失败', icon: 'loading', duration: 20000 });
        wx.redirectTo({
          url: '../../shopCity/shopCity-detial/pay-success/pay-success?ispaysuccess=' + ispaysuccess,
        })
      }
    })
  },
 
 //进入详情
  to_detail:function(e){
    var id = e.currentTarget.dataset.id;
    var status = e.currentTarget.dataset.status;
      wx.navigateTo({
        url: '../../shopCity/shopCity-detial/shopCity-order/shopCity-order?id=' + id + '&status=' + status,
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      shop_name: wx.getStorageSync('shop_name')
    })
    this.getOrderUrl();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
   
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    // wx.navigateBack({
    //   delta: 5
    // })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
})